using System.Collections;
using System.Data;
using PCSComUtils.Framework.ReportFrame.DS;

namespace PCSComUtils.Framework.ReportFrame.BO
{
	public class ViewReportBO
	{
		public ViewReportBO()
		{
			//
			// TODO: Add constructor logic here
			//
		}
		//**************************************************************************              
		///    <Description>
		///       This method uses to get data from specified table
		///    </Description>
		///    <Inputs>
		///       Field, TableName, FilterField[]
		///    </Inputs>
		///    <Outputs>
		///       DataSet
		///    </Outputs>
		///    <Returns>
		///       DataSet
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///       07-Jan-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		//[AutoComplete(true)]
		public DataSet GetDataFromTable(string pstrField, string pstrTableName, string[] pstrFilterFields)
		{
			sys_ReportParaDS dsReportPara = new sys_ReportParaDS();
			return dsReportPara.GetDataFromTable(pstrField, pstrTableName, pstrFilterFields);
		}

		//**************************************************************************              
		///    <Description>
		///       This method uses to execute SqlClause from Parameter and return result as DataSet
		///    </Description>
		///    <Inputs>
		///       SqlClause, WhereClause
		///    </Inputs>
		///    <Outputs>
		///       DataSet
		///    </Outputs>
		///    <Returns>
		///       DataSet
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///       07-Jan-2005
		///       Modified: 09-Jan-2005
		///    </History>
		///    <Notes>
		///		Change return type from string to DataSet
		///    </Notes>
		//**************************************************************************
		//[AutoComplete(true)]
		public DataSet ExecuteSqlClause(string pstrSqlClause, string pstrWhereClause)
		{
			sys_ReportParaDS dsReportPara = new sys_ReportParaDS();
			return dsReportPara.ExecuteSqlClause(pstrSqlClause, pstrWhereClause);
		}
		//**************************************************************************              
		///    <Description>
		///       This method uses to get max report history id
		///    </Description>
		///    <Inputs>
		///       
		///    </Inputs>
		///    <Outputs>
		///       Max ID
		///    </Outputs>
		///    <Returns>
		///       int
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///       09-Jan-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		//[AutoComplete(true)]
		public int GetMaxReportHistoryID()
		{
			sys_ReportHistoryDS dsHistoryPara = new sys_ReportHistoryDS();
			return dsHistoryPara.GetMaxReportHistoryID();
		}

		//**************************************************************************              
		///    <Description>
		///       This method uses to delete data from sys_ReportHistoryPara
		///       but keep 10 last report
		///    </Description>
		///    <Inputs>
		///        HistoryID
		///    </Inputs>
		///    <Outputs>
		///       void
		///    </Outputs>
		///    <Returns>
		///       
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///       09-Jan-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************

		//[AutoComplete(true)]
		public void DeleteHistoryPara(object pobjHistory)
		{
			sys_ReportHistoryVO voReportHistory = (sys_ReportHistoryVO)pobjHistory;
			sys_ReportHistoryParaDS dsReportHistoryPara = new sys_ReportHistoryParaDS();
			dsReportHistoryPara.Delete(voReportHistory.HistoryID);
		}

		//**************************************************************************              
		///    <Description>
		///       This method uses to delete data from sys_ReportHistoryPara
		///       but keep 10 last report
		///    </Description>
		///    <Inputs>
		///        HistoryID
		///    </Inputs>
		///    <Outputs>
		///       void
		///    </Outputs>
		///    <Returns>
		///       
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///       09-Jan-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		//[AutoComplete(true)]
		public void DeleteHistory(object pobjHistory)
		{
			sys_ReportHistoryVO voReportHistory = (sys_ReportHistoryVO)pobjHistory;
			sys_ReportHistoryDS dsReportHistory = new sys_ReportHistoryDS();
			sys_ReportHistoryParaDS dsReportHistoryPara = new sys_ReportHistoryParaDS();
				
			// delete history param first
			dsReportHistoryPara.Delete(voReportHistory.HistoryID);
			// delete history object
			dsReportHistory.Delete(voReportHistory.HistoryID);
			// drop history table from databas
				
			/// HACKED: Thachnn: the first history table is always HistoryTable_1, not HistoryTable_vo.HistoryID.ToString()
			try
			{				
				dsReportHistory.DropHistoryTables(voReportHistory.HistoryID);
			}
			catch
			{				
				dsReportHistory.DropHistoryTables(voReportHistory.TableName);
			}	
		}

		//**************************************************************************              
		///    <Description>
		///       This method uses to drop report history table from system
		///    </Description>
		///    <Inputs>
		///        HistoryID
		///    </Inputs>
		///    <Outputs>
		///       void
		///    </Outputs>
		///    <Returns>
		///       
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///       09-Jan-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		//[AutoComplete(true)]
		public void DropHistoryTables(int pintHistoryID)
		{
			const string METHOD_NAME = "ViewReportBO.DropHistoryTables(int pintHistoryID)";
			sys_ReportHistoryDS dsReportHistory = new sys_ReportHistoryDS();
			dsReportHistory.DropHistoryTables(pintHistoryID);
		}

		//**************************************************************************              
		///    <Description>
		///       This method uses to create report history table and insert data
		///       using SELECT INTO statement.
		///    </Description>
		///    <Inputs>
		///        Sql command
		///    </Inputs>
		///    <Outputs>
		///       DataSet hold records of new table
		///    </Outputs>
		///    <Returns>
		///       DataSet
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///       10-Jan-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		//[AutoComplete(true)]
		public DataSet CreateHistoryTable(string[] parrCommand, int pintMaxID, out string ostrTableName)
		{
			sys_ReportHistoryDS dsHistory = new sys_ReportHistoryDS();
			DataSet dstResult = dsHistory.CreateHistoryTable(parrCommand, pintMaxID, out ostrTableName);
			return dstResult;
		}

		//**************************************************************************
		///    <Description>
		///       This method checks business rule and call Add() method of DS class 
		///    </Description>
		///    <Inputs>
		///       Value object    
		///    </Inputs>
		///    <Outputs>
		///      N/A   
		///    </Outputs>
		///    <Returns>
		///      void
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///     10-Jan-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		//[AutoComplete(true)]
		public void AddHistory(object pobjHistoryVO)
		{
			sys_ReportHistoryDS  dsHistory = new sys_ReportHistoryDS();
			dsHistory.Add(pobjHistoryVO);
		}

		//**************************************************************************
		///    <Description>
		///       This method checks business rule and call Add() method of DS class 
		///    </Description>
		///    <Inputs>
		///       Value object    
		///    </Inputs>
		///    <Outputs>
		///      N/A   
		///    </Outputs>
		///    <Returns>
		///      void
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///     10-Jan-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		//[AutoComplete(true)]
		public void AddHistoryPara(object pobjHistoryParaVO)
		{
			sys_ReportHistoryParaDS  dsHistoryPara = new sys_ReportHistoryParaDS();
			dsHistoryPara.Add(pobjHistoryParaVO);
		}

		//**************************************************************************              
		///    <Description>
		///       This method uses to execute report command and return data set
		///    </Description>
		///    <Inputs>
		///        Report Command
		///    </Inputs>
		///    <Outputs>
		///       DataSet
		///    </Outputs>
		///    <Returns>
		///       DataSet
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///       10-Jan-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		public DataSet ExecuteReportCommand(string pstrReportCommand)
		{
			DataSet dstResult = new DataSet();
			sys_ReportDS dsReport = new sys_ReportDS();
			dstResult = dsReport.ExecuteReportCommand(pstrReportCommand);
			return dstResult;
		}

		//**************************************************************************              
		///    <Description>
		///       This method uses to execute report command and return data set
		///    </Description>
		///    <Inputs>
		///        Report Command
		///    </Inputs>
		///    <Outputs>
		///       DataSet
		///    </Outputs>
		///    <Returns>
		///       DataSet
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///       10-Jan-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		//[AutoComplete(true)]
		public DataSet ExecuteReportCommand(string pstrReportCommand, string pstrTableName)
		{
			DataSet dstResult = new DataSet();
			sys_ReportDS dsReport = new sys_ReportDS();
			dstResult = dsReport.ExecuteReportCommand(pstrReportCommand, pstrTableName);
			return dstResult;
		}

		//**************************************************************************              
		///    <Description>
		///       This method uses to get sum of width of all fields of a report
		///       in order to setting report width
		///    </Description>
		///    <Inputs>
		///        ReportID
		///    </Inputs>
		///    <Outputs>
		///      sum of width
		///    </Outputs>
		///    <Returns>
		///       int
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///       18-Jan-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		//[AutoComplete(true)]
		public int SumFieldsWidth(string pstrReportID)
		{
			sys_ReportFieldsDS dsField = new sys_ReportFieldsDS();
			return dsField.SumFieldsWidth(pstrReportID);
		}

		//**************************************************************************              
		///    <Description>
		///       This method uses to get all data from History Table in order to show
		///       executed report
		///    </Description>
		///    <Inputs>
		///        History Table Name
		///    </Inputs>
		///    <Outputs>
		///      DataTable
		///    </Outputs>
		///    <Returns>
		///       DataTable
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///       20-Jan-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		//[AutoComplete(true)]
		public DataTable GetDataFromHistoryTable(string pstrHistoryTableName)
		{
			sys_ReportHistoryDS dsHistory = new sys_ReportHistoryDS();
			return dsHistory.GetDataFromHistoryTable(pstrHistoryTableName);
		}
		//**************************************************************************              
		///    <Description>
		///       This method uses to get max report history id
		///    </Description>
		///    <Inputs>
		///       
		///    </Inputs>
		///    <Outputs>
		///       Max ID
		///    </Outputs>
		///    <Returns>
		///       int
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///       09-Jan-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		/// HACK: Thachnn: I rem this line because of ambiguous problem we meet with COM+ transation
		/// while we create new COM+ AppDomain to execute the new compiled assembly to build the report
		/// but it has no effect
		//[AutoComplete(true)]
		public ArrayList GetHistoryReportByUser(string pstrUsername)
		{
			sys_ReportHistoryDS dsHistoryPara = new sys_ReportHistoryDS();
			return dsHistoryPara.GetHistoryReportByUser(pstrUsername);
		}

		/// <summary>
		/// Thachnn: 01/Oct/2005
		/// Create a new table (with provided name) in the database from the provided System.Data.DataTable object
		/// </summary>
		/// <param name="pdtb">source data table, contain schema and data to create in the database</param>
		/// <param name="pstrDBNewTableName">new table name to create in the database</param>		
		/// <returns>true if function finish SUCCESSfully</returns>
		//[AutoComplete(true)]
		public bool PushDataTableIntoNewDatabaseTable(DataTable pdtb, string pstrDBNewTableName)
		{
			sys_ReportHistoryDS dsHistoryPara = new sys_ReportHistoryDS();
			return dsHistoryPara.PushDataTableIntoNewDatabaseTable(pdtb, pstrDBNewTableName);
		}

		/// <summary>
		/// Get All field and group of a report
		/// </summary>
		/// <param name="pstrReportID">ReportID</param>
		/// <returns>List of sys_FieldGroupDetailVO object</returns>
		//[AutoComplete(true)]
		public ArrayList GetAllFieldGroups(string pstrReportID)
		{
			Sys_FieldGroupDetailDS dsFieldGroup = new Sys_FieldGroupDetailDS();
			return dsFieldGroup.List(pstrReportID);
		}

		/// <summary>
		/// List all groups of a report
		/// </summary>
		/// <param name="pstrReportID">ReportID</param>
		/// <returns>List of sys_FieldGroupVO</returns>
		//[AutoComplete(true)]
		public ArrayList GetAllGroups(string pstrReportID)
		{
			Sys_FieldGroupDS dsFieldGroup = new Sys_FieldGroupDS();
			return dsFieldGroup.GetAllGroups(pstrReportID);
		}
		//[AutoComplete()]
		public object GetReportByReportID(string pstrReportID)
		{
			return (new sys_ReportDS()).GetObjectVO(pstrReportID);
		}

	}
}
